#include <string>
#include<thread>
#include<iostream>
// 方式1，使用局部静态变量, 饿汉式
class Single2Hungry{
    private:
        Single2Hungry(const std::string value): value_(value) {}
        Single2Hungry(const Single2Hungry&) = delete;
        Single2Hungry &operator=(const Single2Hungry&) = delete;
        static Single2Hungry *single;
        std::string value_;
    public:
        static Single2Hungry *GetInstance(const std::string& value = ""){
            return single;
        }
        std::string value() const { return value_; }
    };

Single2Hungry* Single2Hungry::single = new Single2Hungry("Default");

void ThreadFoo(){
    std::this_thread::sleep_for(std::chrono::microseconds(1000));
    Single2Hungry * single = Single2Hungry::GetInstance("foo");
    std::cout << single->value() << "\n";
}

void ThreadBar(){
    std::this_thread::sleep_for(std::chrono::microseconds(1000));
    Single2Hungry * single = Single2Hungry::GetInstance("bar");
    std::cout << single->value() << "\n";
}

int main(){
    std::cout<<"如果看到相同的值，说明单例被重用了 (正确!)\n" <<
                "如果看到不同的值，说明创建了2个单例 (错误!)\n\n" <<
                "运行结果:\n"; 
    std::thread t1(ThreadBar);
    std::thread t2(ThreadFoo);
    t1.join();
    t2.join();
    return 0;
}